package com.yuke.cloud.service.opc.controller.rpc;

import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.opc.service.OpcSendNotifyFeignApi;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.dto.UserLoginInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.opc.service.OpcSendNotifyService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(value = "API - OpcSendNotifyFeignApi", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SendNotifyFeignClient extends BaseController implements OpcSendNotifyFeignApi {
    @Autowired
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private OpcSendNotifyService opcSendNotifyService;


    @Override

    public Integer sendnotify(@RequestParam("userId") String userId, @RequestParam("title") String title, @RequestParam("content") String content,@RequestParam("isnotify")int isnotify) {
        //System.out.println("userid"+userId);
        //System.out.println("title"+title);
        //System.out.println("content"+content);
        //System.out.println("isnotify"+isnotify);
        Wrapper<List<UserLoginInfoDto>> userDeviceToken = uacUserFeignApi.getUserDeviceToken(Long.valueOf(userId));
        List<UserLoginInfoDto> result = userDeviceToken.getResult();
        for (UserLoginInfoDto dot : result) {
            String deviceId = dot.getDeviceId();
            String os = dot.getOs();
            System.out.println("os"+os);
            System.out.println("deviceid"+deviceId);
            if (os!=null&&os.toLowerCase().contains("android")) {
                if (isnotify == 0) {
                    //0表示通知栏消息
                    opcSendNotifyService.sendNotifyByUser(deviceId, title, content, false, true);
                } else {
                    opcSendNotifyService.sendNotifyByUser(deviceId, title, content, true, true);
                }
            } else if (os!=null&&os.toLowerCase().contains("ios")){
                if (isnotify == 0) {
                    //1表示应用内消
                    opcSendNotifyService.sendNotifyByUser(deviceId, title, content, false, false);
                } else {
                    opcSendNotifyService.sendNotifyByUser(deviceId, title, content, true, false);
                }
            }
        }
        return null;
    }
}
